#!/usr/bin/env python
# encoding: utf-8
'''
@author: wangjianrong
@software: pycharm
@file: draw_zh.py
@time: 2020/9/22 10:22
@desc:在cv2图片中显示中文
'''

from PIL import Image,ImageDraw,ImageFont
import numpy as np
import cv2

def puttext_cn(img,text,pt):
    if (isinstance(img, np.ndarray)):  # 判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(img)
    # 字体的格式
    fontStyle = ImageFont.truetype(
        "simhei.ttf", 40, encoding="utf-8")
    # 绘制文本
    draw.text(pt, text, (255,0,0), font=fontStyle)
    # 转换回OpenCV格式
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)